// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import QtDesktop 0.1
import "delegates"
import "models"
import "views"

Rectangle {
    id: plannerForm
    property int draggedElementIndex: 0
    MouseArea {
        id: mouse
        anchors.fill: parent
        onPressed: {
            plannerForm.draggedElementIndex = views.activeView.indexAt(mouseX, mouseY)
            console.log(plannerForm.draggedElementIndex + ' (' + mouseX + ', ' + mouseY + ')')
        }
        onReleased: {
            plannerForm.state = "idle"
        }
    }
    Column {
        spacing: 10
        ToolBar {
            id: searchToolBar
            height: 40
            width: plannerForm.width
            Rectangle {
                y: 10
                height: 20
                width: 100
                border.color: "lightgray"
                border.width: 1
                radius: 3

                TextField {
                    id: searchText
                    KeyNavigation.tab: filter
                    KeyNavigation.backtab: todayJobs
                    placeholderText: qsTr("Search...")
                }
            }
            ComboBox {
                id: filter;
                anchors.right: parent.right
                model: ListModel{
                    ListElement {text: "Today"}
                    ListElement {text: "Week"}
                    ListElement {text: "Mounth"}
                    ListElement {text: "Year"}
                    ListElement {text: "All tasks"}
                }
                width: 100
                KeyNavigation.tab: views.activeView
                KeyNavigation.backtab: searchText
            }
        }
        Rectangle {
            id: views
            width: plannerForm.width
            height: plannerForm.height - searchToolBar.height - 20

            property Item activeView: weekJobs
            Row {
                TodayJobs {
                    id: todayJobs

                    visible: false

                    width: views.width
                    height: views.height

                    calcHeight: views.height/25 < 40 ? 40 : views.height/25

                    KeyNavigation.tab: searchText
                    KeyNavigation.backtab: filter

                    onAddJob: {
                        plannerForm.state = "addJobState"
                        addJobForm.setTime(time)
                    }
                }
                WeekJobs {
                    id: weekJobs

                    width: views.width
                    height: views.height
                    calcWidth: views.width/7 < 40 ? 40 : views.width/7

                    KeyNavigation.tab: searchText
                    KeyNavigation.backtab: filter
                    onAddJob: {
                        plannerForm.state = "addJobState"
                        addJobForm.setTime(fromTime)
                    }
                }
                MounthJobs {
                    id: mounthJobs

                    visible: false

                    //width: views.width
                    height: views.height

                    KeyNavigation.tab: searchText
                    KeyNavigation.backtab: filter
                    onAddJob: {
                        plannerForm.state = "addJobState"
                        addJobForm.setTime(fromTime)
                    }
                }
                YearJobs {
                    id: yearJobs

                    visible: false

                    width: views.width
                    height: views.height

                    KeyNavigation.tab: searchText
                    KeyNavigation.backtab: filter
                    onAddJob: {
                        plannerForm.state = "addJobState"
                        addJobForm.setTime(time)
                    }
                }
                GantJobs {
                    id: allJobs

                    visible: false

                    width: views.width
                    height: views.height

                    KeyNavigation.tab: searchText
                    KeyNavigation.backtab: filter
                }
            }
            states: [
                State {
                    name: "today"
                    when: filter.selectedIndex === 0
                    PropertyChanges {
                        target: todayJobs
                        visible: true
                        focus: true
                    }
                    PropertyChanges {
                        target: weekJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: mounthJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: yearJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: allJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: views
                        activeView: todayJobs
                    }
                },
                State {
                    name: "week"
                    when: filter.selectedIndex === 1
                    PropertyChanges {
                        target: todayJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: weekJobs
                        visible: true
                        focus: true
                    }
                    PropertyChanges {
                        target: mounthJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: yearJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: allJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: views
                        activeView: weekJobs
                    }
                },
                State {
                    name: "mounth"
                    when: filter.selectedIndex === 2
                    PropertyChanges {
                        target: todayJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: weekJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: mounthJobs
                        visible: true
                        focus: true
                    }
                    PropertyChanges {
                        target: yearJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: allJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: views
                        activeView: mounthJobs
                    }
                },
                State {
                    name: "year"
                    when: filter.selectedIndex === 3
                    PropertyChanges {
                        target: todayJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: weekJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: mounthJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: yearJobs
                        visible: true
                        focus: true
                    }
                    PropertyChanges {
                        target: allJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: views
                        activeView: yearJobs
                    }
                },
                State {
                    name: "all"
                    when: filter.selectedIndex === 4
                    PropertyChanges {
                        target: todayJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: weekJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: mounthJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: yearJobs
                        visible: false
                    }
                    PropertyChanges {
                        target: allJobs
                        visible: true
                        focus: true
                    }
                    PropertyChanges {
                        target: views
                        activeView: allJobs
                    }
                }
            ]
            state: "week"
        }
    }
    /*DraggedItem {
        id: dragItem
        x: mouse.mouseX
        y: mouse.mouseY
        visible: false
    }*/
    AddJobForm {
        id: addJobForm
        x: parent.width/2 - width/2
        y: parent.height/2 - height/2
        onCancel: plannerForm.state = "idle"
        onAdd: {
            jobsModel.addJob(name, description, addJobForm.fromTime, longTime)
            plannerForm.state = "idle"
        }
    }

    states: [
        State {
            name: "idle"
            /*PropertyChanges {
                target: dragItem
                visible: false
            }*/
            PropertyChanges {
                target: addJobForm
                visible: false
            }
        },
        State {
            name: "drag"
            when: mouse.pressed && plannerForm.draggedElementIndex !== -1
            /*PropertyChanges {
                target: dragItem
                visible: true
            }*/
            PropertyChanges {
                target: addJobForm
                visible: false
            }
        },
        State {
            name: "addJobState"
            /*PropertyChanges {
                target: dragItem
                visible: false
            }*/
            PropertyChanges {
                target: addJobForm
                state: "edit"
                visible: true
            }
        }
    ]
    state: "idle"

}
